package com.huiyou.utils;

import com.huiyou.model.AdminModel;
import com.huiyou.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class DateUtils {

    public final static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_FORMAT_NOTIME = "yyyy-MM-dd";
    @Autowired
    private  AdminService adminService;

    public static void main(String[] args) throws ParseException {
        compareDate("2019-09-21");

        List<AdminModel> list = new ArrayList<>(3);
        list.add(new AdminModel());
        list.add(new AdminModel());
        list.add(new AdminModel());

        list.stream().map(new Function() {
            @Override
            public Object apply(Object o) {
                AdminModel adminModel = (AdminModel) o;
                adminModel.setUsername("sss");
                return adminModel;
            }
        }).collect(Collectors.toList());
        System.out.println(list);

    }

    public static boolean compareDate(String birthday) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = dateFormat.format(new Date());
        String[] todayYMD = today.split("-");
        String[] ymd = birthday.split("-");
        int count = 0;
        for (int i = 1; i < ymd.length; i++) {
//			System.out.println(ymd[i]);
//			System.out.println(todayYMD[i]);
            if (ymd[i].equals(todayYMD[i])) {
                count++;
            }
        }
        if (count >= 2) {
            System.out.println("今天是你的生日" + count);
            return true;
        } else
            return false;
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getFormatTime(Date date, String pattern) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            return simpleDateFormat.format(date).toString();
        } catch (Exception e) {
            return "";
        }
    }

    public static Date getFormatDate(String date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            return simpleDateFormat.parse(date);
        } catch (Exception e) {
            return null;
        }
    }

    public static Date getFormatDate(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 偏移时间（分钟）
     *
     * @param date   时间
     * @param offset 偏移量，负数代表往前，正数代表往后
     * @return
     */
    public static Date offSetMin(Date date, int offset) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, offset);
        return cal.getTime();
    }

    /**
     * @param date
     * @param field
     * @param offset
     * @return
     */
    public static Date offSet(Date date, int field, int offset) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field, offset);
        return cal.getTime();

    }

    public static String addSecond(Integer second) {
        Long currentTime = (long) (System.currentTimeMillis() + second * 1000);
        Date date = new Date(currentTime);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = df.format(date);

        return nowTime;
    }

    public static String addDay(Integer days) {
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, days);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = df.format(ca.getTime());

        return nowTime;
    }

    public static Long parseDateTomillion(Date date) {
        long millionSeconds = 0;
        try {
            String nowTime = getFormatTime(date, "yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            millionSeconds = format.parse(nowTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return millionSeconds;
    }


}
